package com.ruoyi.framework.reptile.webmagic.jingcai;

import com.alibaba.fastjson.JSON;
import com.ruoyi.common.enums.JcTeamType;
import com.ruoyi.common.enums.Yn;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.framework.reptile.webmagic.jingcai.rank.cba.SinaCbaRankPipeline;
import com.ruoyi.framework.reptile.webmagic.jingcai.rank.dto.QqBaseRes;
import com.ruoyi.framework.reptile.webmagic.jingcai.rank.dto.QqFootBallRankDetail;
import com.ruoyi.system.domain.JcTeam;
import com.ruoyi.system.service.IJcScoreService;
import com.ruoyi.system.service.IJcTeamService;
import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.math.BigDecimal;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

public abstract class AbstractJingCaiPipeline {

    private static final Logger log = LoggerFactory.getLogger(AbstractJingCaiPipeline.class);

    /**
     * 获取竞彩球队
     * @return
     */
    protected IJcTeamService getJcTeamService() {
        return SpringUtils.getBean(IJcTeamService.class);
    }

    /**
     * 获取竞彩大小分
     * @return
     */
    protected IJcScoreService getJcScoreService() {
        return SpringUtils.getBean(IJcScoreService.class);
    }


    protected List<JcTeam> findJcTeamByParam(JcTeam queryParam) {
        if (Objects.isNull(queryParam)) {
            queryParam = new JcTeam();
        }
        queryParam.setYn(Yn.ENABLE.getCode());
        List<JcTeam> jcTeams = getJcTeamService().selectJcTeamList(queryParam);
        return jcTeams;
    }

    /**
     * 处理竞彩球队
     * @param queryParam
     */
    protected void handleJcTeam(JcTeam queryParam, List<JcTeam> dataList) {
        if (CollectionUtils.isEmpty(dataList)) {
            log.info("需要处理的竞彩球队为空，直接返回");
            return;
        }
        List<JcTeam> jcTeamList = findJcTeamByParam(queryParam);
        Map<String, JcTeam> teamMap = CollectionUtils.isEmpty(jcTeamList) ? new HashMap<>() : jcTeamList.stream().collect(Collectors.toMap(JcTeam::getTeam, Function.identity(), (k1,k2) -> k2));
        for (JcTeam jcTeam : dataList) {
            JcTeam oldTeam = teamMap.get(jcTeam);
            if (Objects.isNull(oldTeam)) {
                // 历史数据为空直接保存
                getJcTeamService().insertJcTeam(jcTeam);
            } else {
                // 那就证明历史数据存在，需要更新
                jcTeam.setId(oldTeam.getId());
                getJcTeamService().updateJcTeam(jcTeam);
            }
        }
    }

    /**
     * 构建腾讯更新竞彩结果的入参
     * @param qqBaseRes
     * @return
     */
    protected List<JcTeam> buildUpdateListByQq(QqBaseRes qqBaseRes, String season, Integer type) {
        List<JcTeam> updateList = qqBaseRes.getData().get(0).getRank().get(0).getRows().stream().map(rankDetail -> {
            QqFootBallRankDetail.QqFootBallRankDetailData data = rankDetail.getData();
            QqFootBallRankDetail.QqFootBallRankDetailConfig config = rankDetail.getConfig();
            JcTeam jcTeam = new JcTeam();
            jcTeam.setTeam(config.getName());
            jcTeam.setType(type);
            jcTeam.setRanking(Integer.valueOf(config.getSerial().replace(".", "")));
            jcTeam.setSeason(season);
            String[] winPlanLost = splitResult(data.getWinPlanLost());
            jcTeam.setHomeWins(Integer.valueOf(winPlanLost[0]));
            jcTeam.setHomeLose(Integer.valueOf(winPlanLost[2]));
            // 足球这里存平局到客胜
            jcTeam.setOpponentWins(Integer.valueOf(winPlanLost[1]));
            jcTeam.setOpponentLose(0);
            String[] getAndLoss = splitResult(data.getGetAndLoss());
            jcTeam.setAvgScores(new  BigDecimal(getAndLoss[0]));
            jcTeam.setAvgLoseScores(new  BigDecimal(getAndLoss[1]));
            jcTeam.setYn(Yn.ENABLE.getCode());
            jcTeam.setCreator("webmagic");
            jcTeam.setModifier("webmagic");
            return jcTeam;
        }).collect(Collectors.toList());
        return updateList;
    }

    private String[] splitResult(String s) {
        String[] split = s.split("/");
        return split;
    }
}
